package com.keensense.vcm.internal;

import com.keensense.vcm.HttpMethod;
import com.keensense.vcm.common.auth.CredentialsProvider;
import com.keensense.vcm.common.comm.RequestMessage;
import com.keensense.vcm.common.comm.ServiceClient;
import com.keensense.vcm.common.parser.ResponseParsers;
import com.keensense.vcm.model.*;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lanjian
 * @date 2020/7/27
 */
public class StaticLibrariesOperation extends VCMOperation {

    public StaticLibrariesOperation(ServiceClient client, CredentialsProvider credsProvider) {
        super(client, credsProvider);
    }


    /**
     * 静态库查询人员信息
     *
     * @param getPeoplesRequest
     * @return
     */
    public GetPeoplesResponse getPeoples(GetPeoplesRequest getPeoplesRequest) {
        String xml = getXmlFromObject(getPeoplesRequest, GetPeoplesRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request = new VCMRequestMessageBuilder(getInnerClient()).setInputStream(in)
                .setInputSize(xml.getBytes().length).setEndpoint(endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.STATICLIBRARIES);
        GetPeoplesResponse result = doOperation(request, ResponseParsers.getPeoplesResponseParser, true);
        return result;
    }


    /**
     * 查询静态库列表
     *
     * @param request
     * @return
     */
    public ListStaticLibrariesResponse listStaticLibraries(ListStaticLibrariesRequest request) {
        StringBuffer resourcePath = new StringBuffer(VCMResourcePathConstants.LIST_STATICLIBRARIES);
        resourcePath.append("?no=").append(request.getNo()).append("&size=").append(request.getSize())
                .append("&sort=").append(request.getSort().name()).append("&ordername=name")
                .append("&tag=").append(request.getTag()).append("&userId").append(request.getUserId());
        Map<String, String> params = new HashMap<>();
        params.put("no", String.valueOf(request.getNo()));
        params.put("size", String.valueOf(request.getSize()));
        params.put("sort", request.getSort().name());
        params.put("ordername", "name");
        params.put("tag", request.getTag());
        params.put("userId", request.getUserId());

        RequestMessage requestMessage = new VCMRequestMessageBuilder(getInnerClient()).setEndpoint(endpoint)
                .setParameters(params).setMethod(HttpMethod.GET).build();
        requestMessage.setResourcePath(VCMResourcePathConstants.LIST_STATICLIBRARIES);
        return doOperation(requestMessage, ResponseParsers.listStaticLibrariesResponseParser, true);
    }
}
